<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class CommonEntryModel extends AppModel
{
	
	// CATEGORIES // ---------------------------
	
	// return categories array for entry
	function &getCategoryById($record_id) {
/*
		$sql = "SELECT category_id AS id, category_id FROM {$this->tbl->entry_to_category} 
		WHERE entry_id IN ($record_id)";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->GetAssoc();
*/
		
		$data = array();
		$sql = "SELECT category_id, is_main FROM {$this->tbl->entry_to_category} 
		WHERE entry_id IN ($record_id)";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		while($row = &$result->FetchRow()) {
			$data[$row['is_main']][$row['category_id']] = $row['category_id'];
		}
		
		return $data;
	}
	
	
	// get array with categories names to use in list records
	function &getCategoryByIds($record_id) {
		
		$sql = "
		SELECT 
			ec.entry_id, 
			c.name,
			ec.is_main
			
		FROM 
			{$this->tbl->category} c, 
			{$this->tbl->entry_to_category} ec
		WHERE 1
			AND ec.entry_id IN ($record_id) 
			AND c.id = ec.category_id
			ORDER BY ec.is_main DESC";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		while($row = $result->FetchRow()){
			$data[$row['entry_id']][] = $row['name'];
		}
		
		return $data;
	}
	
	
	// range for form select
	function &getCategorySelectRange($rows = false) {
		if(!$rows) {
			$rows = $this->getCategoryRecords();
		}
		return $this->cat->getSelectRange($rows);
	}
	
	
	function getChilds($rows, $id) {
		
		$tree = &new TreeHelper();
		foreach($rows as $k => $row) {
			$tree->setTreeItem($row['id'], $row['parent_id']);
		}
		
		return $tree->getChildsById($id);
	}

	
	// ENTRY TO MEMBER ROLE // -----
	
	function getMemberRoleById($record_id, $list_view = false) {

			$select = ($list_view) ? 'dv.data_value AS k, r.title AS v' : 'r.id, r.id AS id1';
			return $this->dv_manager->getDataById($record_id, 
		                                   		 $this->member_rore_rule, 
		                                         "{$this->tbl->role} r", 
										         'r.id',
												 $select,
												 $list_view);
	}
	
	
	function saveMemberRoleToEntry($obj, $record_id) {
		$this->dv_manager->saveData($obj->getMemberRole(), $record_id, $this->member_rore_rule);
	}
	
	
	function deleteMemberRoleToEntry($record_id) {
		$this->dv_manager->deleteData($record_id, $this->member_rore_rule);
	}
	
	
	function getMemberRoleSelectRange() {
		$r = new RoleModel();
		return $r->getSelectRange('member');
	}		
}
?>